package top.lingkang.finalpool;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

public class FinalDataSource implements DataSource {

    public FinalDataSource() {
        this.id = System.currentTimeMillis();
    }

    /**
     * 生成一个id
     */
    private long id;
    private Connection conn;
    private long lastGetTime = 0L;
    private int CONNECTION_OUT_TIME = 60000;

    public void setConnection(Connection conn) {
        lastGetTime = System.currentTimeMillis();
        this.conn = conn;
    }

    public long getLastGetTime() {
        return lastGetTime;
    }

    public long getId() {
        return id;
    }

    /**
     * 关闭链接
     */
    public void closeConnection() {
        try {
            conn.close();
            System.out.println("关闭数据源完成");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public Connection getConnection() throws SQLException {
        lastGetTime = System.currentTimeMillis();
        return conn;
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {
        CONNECTION_OUT_TIME = seconds;
    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return CONNECTION_OUT_TIME;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }

}
